Skip to content

gh-158032: Keep the element tag alive during Element.iter comparison - #158044

Merged
serhiy-storchaka merged 4 commits into
python:mainfrom
lul-cas:fix-issue-158032
Nov 12, 2026
Merged

serhiy-storchaka merged 4 commits into
python:mainfrom
lul-cas:fix-issue-158032

Conversation

@lul-cas

@lul-cas lul-cas commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Element.iter compared each element's tag through a borrowed reference. A reentrant eq can replace that tag and free it while the comparison is still using it.

Hold a strong reference for the duration of PyObject_RichCompareBool, as Element.find already does.

Fixes #158032.

@serhiy-storchaka

Copy link
Copy Markdown
Member

The fix is correct, and matches what find(), findall() and remove() already do. Element.iter() was the only place which was left.

The test can be simpler: iterating and checking the result is enough, without __del__ and the lists. Please also mention in the NEWS entry that it was a crash.

@lul-cas

lul-cas commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka. Done. I've simplified the test and mentioned the crash in the NEWS entry.

@serhiy-storchaka

Copy link
Copy Markdown
Member

Sorry, my suggestion was wrong: please restore the previous test.

On a debug build it crashes with SIGSEGV without the fix and passes with it, while the simplified test passes either way: nothing in it looks at the freed tag, so only ASan sees the use-after-free. The isinstance(other, Tag) check against destroyed is what detects it.

@lul-cas

lul-cas commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka. Ok! Restored.

@serhiy-storchaka

Copy link
Copy Markdown
Member

LGTM. 👍

@serhiy-storchaka
serhiy-storchaka merged commit 78a70f8 into python:main Nov 12, 2026
61 checks passed
@serhiy-storchaka serhiy-storchaka added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 12, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @lul-cas for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Thanks @lul-cas for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Nov 12, 2026

Copy link
Copy Markdown

GH-161913 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 12, 2026
@bedevere-app

bedevere-app Bot commented Nov 12, 2026

Copy link
Copy Markdown

GH-161914 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Nov 12, 2026
serhiy-storchaka pushed a commit that referenced this pull request Nov 12, 2026
…arison (GH-158044) (GH-161914)

(cherry picked from commit 78a70f8)

Co-authored-by: Lucas <lucasefernandes333@gmail.com>
serhiy-storchaka pushed a commit that referenced this pull request Nov 14, 2026
…arison (GH-158044) (GH-161913)

(cherry picked from commit 78a70f8)

Co-authored-by: Lucas <lucasefernandes333@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

uaf in _elementtree Element.iter(tag)

2 participants